package com.squirrel.common.controller;

import com.squirrel.common.annotation.AuthToken;
import com.squirrel.common.param.user.*;
import com.squirrel.common.service.UserService;
import com.squirrel.common.response.ResponseResult;
import com.squirrel.common.response.ResponseResultUtils;
import com.squirrel.common.response.code.CodeEnum;
import com.squirrel.common.vo.LoginVO;
import com.squirrel.common.vo.user.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  UserController 用户前端控制器
 * </p>
 *
 * @author 关远键
 * @since 2021/11/10 14:25
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户控制器")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("密码登录")
    public ResponseResult<LoginVO> login(HttpServletResponse response, LoginParam loginParam) {
        return ResponseResultUtils.success(CodeEnum.LOGIN_SUCCESS, userService.login(response, loginParam));
    }


    @PostMapping("/getAuthCode")
    @ApiOperation("获取邮箱验证码")
    public ResponseResult<String> getAuthCode(@RequestParam String mail) {
        String codeResult = userService.createAuthCode(mail);
        return ResponseResultUtils.success(CodeEnum.SUCCESS, codeResult);
    }


    @PostMapping("/mailLogin")
    @ApiOperation("邮箱登录")
    public ResponseResult login(HttpServletResponse response, LoginByMailParam loginByMailParam) {
        return ResponseResultUtils.success(CodeEnum.LOGIN_SUCCESS, userService.loginByMail(response, loginByMailParam));
    }

    @PostMapping("/reg")
    @ApiOperation("邮箱注册")
    public ResponseResult reg(RegisterParam registerParam) {
        return ResponseResultUtils.success(CodeEnum.REGISTER_SUCCESS, userService.register(registerParam));
    }

    @PostMapping("/loginByPhone")
    @ApiOperation("短信验证登录（如非必要，不使用，要钱的！！！）")
    public ResponseResult<LoginVO> loginByPhone(HttpServletResponse response, PhoneLoginParam loginParam) {
        return ResponseResultUtils.success(CodeEnum.LOGIN_SUCCESS, userService.loginByPhone(response, loginParam));
    }

    @PostMapping("/getCodeByPhone")
    @ApiOperation("手机号验证码，首次获取时注册（有效期5分钟，如非必要，不使用，要钱的！！！）")
    public ResponseResult<Boolean> getCodeByPhone(PhoneParam phoneParam) {
        return ResponseResultUtils.success(userService.getCodeByPhone(phoneParam));
    }

//    @PatchMapping("/modify")
@PostMapping(value = "/modify", consumes = "multipart/*", headers = "content-type=multipart/form-data")
@ApiOperation("修改信息")
//    @AuthToken
public ResponseResult<String> modify(@RequestPart("avatar") MultipartFile avatar, ModifyParam param) {
    return ResponseResultUtils.success(CodeEnum.MODIFY_SUCCESS, userService.modify(avatar, param));
}

    @GetMapping("/getAuthorInfo/{authorId}")
    @ApiOperation("作者信息")
    public ResponseResult<UserInfoVO> getAuthorInfo(@PathVariable("authorId") Integer authorId) {
        return ResponseResultUtils.success(userService.getAuthorInfo(authorId));
    }

}
